// import { validateCustomPositiveInt, validateCustomADecimal, validateCustomTwoDecimal } from '@/utils/validate'
import { test2Decimal, testPositiveInt } from '@/utils/validator'
import { valid2Decimal, validPositiveInt } from '@/utils/validator'

export default {
  data() {
    const validateActivityDate = (rule, value, callback) => {
      if (this.$isNull(value) || value.length !== 2) {
        callback(new Error('请选择日期'))
      } else {
        callback()
      }
      if (this.form.type === 'SEC_KILL') this.$refs.form.validateField(['bookingTime'])
    }

    //  有效时间段
    const validatePeriodTimeList = (rule, value, callback) => {
      const startTimeArr = []
      const endTimeArr = []

      value.some(v => {
        if (!v.beginTime || !v.endTime) {
          callback(new Error('请选择时间'))
          return true
        } else if (v.beginTime !== v.endTime && v.beginTime < v.endTime) {
          startTimeArr.push(v.beginTime)
          endTimeArr.push(v.endTime)
        } else {
          callback(new Error('开始时间不可大于等于结束时间'))
        }
      })
      // 排序
      const startTimeSortArr = startTimeArr.sort()
      const endTimeSortArr = endTimeArr.sort()
      // 判断交叉重复
      let flag = false
      for (let k = 1; k < startTimeSortArr.length; k++) {
        if (startTimeSortArr[k] <= endTimeSortArr[k - 1]) {
          flag = true
        }
      }
      if (flag) {
        callback(new Error('时间区间不可连续或交叉重叠'))
      } else {
        callback()
      }
      // if (this.form.type === 'SEC_KILL') this.$refs.form.validateField(['bookingTime'])
      // const startTimeArr = []
      // const endTimeArr = []
      // value.some(v => {
      //   if (!v.activityTime || v.activityTime.length !== 2) {
      //     callback(new Error('请选择时间'))
      //     return true
      //   } else {
      //     startTimeArr.push(v.activityTime[0])
      //     endTimeArr.push(v.activityTime[1])
      //   }
      // })
      // // 排序
      // const startTimeSortArr = startTimeArr.sort()
      // const endTimeSortArr = endTimeArr.sort()
      // // 判断交叉重复
      // let flag = false
      // for (let k = 1; k < startTimeSortArr.length; k++) {
      //   if (startTimeSortArr[k] <= endTimeSortArr[k - 1]) {
      //     flag = true
      //   }
      // }
      // if (flag) {
      //   callback(new Error('时间区间存在交叉重叠'))
      // } else {
      //   callback()
      // }
      // if (this.form.type === 'SEC_KILL') this.$refs.form.validateField(['bookingTime'])
    }

    // 会员等级
    const validateUserLimitCards = (rule, value, callback) => {
      if (this.form.configForm.userLimit !== 2) {
        callback()
      } else if (value.length === 0) {
        callback(new Error('请选择'))
      } else {
        callback()
      }
    }
    // 预约时间
    const validateBookingTime = (rule, value, callback) => {
      if (this.form.type === 'SEC_KILL' && this.form.booking) {
        const endTime = []
        this.form.periodTimeList.forEach(v => {
          if (v.beginTime && v.endTime) endTime.push(v.endTime)
        })
        endTime.sort((a, b) => {
          return b - a
        })
        if (this.$isNull(value)) {
          callback(new Error('请选择'))
        } else if (this.form.activityDate && this.form.activityDate.length === 2 && endTime.length && value) {
          console.log(value, this.form.activityDate[1] + ' ' + (endTime[0] === '24:00' ? '23:59' : endTime[0]))
          if (this.$momentMini(value).isSameOrBefore(this.form.activityDate[1] + ' ' + (endTime[0] === '24:00' ? '23:59' : endTime[0]))) {
            callback()
          } else {
            callback(new Error('秒杀截止时间应该小于活动截止时间'))
          }
        } else {
          callback()
        }
      } else {
        callback()
      }
    }

    const validateGroupExpireTimeArray = (rule, value, callback) => {
      let flag = true
      value.map(v => {
        if (!this.$isNull(v) && !testPositiveInt(v, 1, 9999999, [0])) flag = false
      })

      if (flag) {
        if (value[0] > 30 || value[1] > 23 || value[2] > 59) {
          callback(new Error('至少设置一个时间，请输入整数，有效期天最大值30，小时最大值23，分最大值59'))
        } else if (Number(value[0]) + Number(value[1]) + Number(value[2]) === 0) {
          callback(new Error('至少设置一个时间，请输入整数，有效期天最大值30，小时最大值23，分最大值59'))
        } else {
          callback()
        }
      } else {
        callback(new Error('至少设置一个时间，请输入整数，有效期天最大值30，小时最大值23，分最大值59'))
      }
    }

    const validateDeliveryList = (rule, value, callback) => {
      const flag = this.form.deliveryList.some(v => {
        if (v.supply) return true
      })
      if (!flag) {
        callback(new Error('请至少开启一种订单支持方式'))
      } else {
        callback()
      }
    }

    const validateGroupAutoAfterCreate = (rule, value, callback) => {
      console.log(this.form.configForm.groupAutoRuleCheckBox.includes('groupAutoAfterCreate'))
      if (this.form.configForm.groupAutoRuleCheckBox.includes('groupAutoAfterCreate')) {
        testPositiveInt(value) ? callback() : callback(new Error('请输入1~999'))
      } else {
        callback()
      }
    }

    const validateGroupAutoBeforeClose = (rule, value, callback) => {
      if (this.form.configForm.groupAutoRuleCheckBox.includes('groupAutoBeforeClose')) {
        testPositiveInt(value) ? callback() : callback(new Error('请输入1~999'))
      } else {
        callback()
      }
    }

    const validateGroupAutoBeforeNum = (rule, value, callback) => {
      if (this.form.configForm.groupAutoRuleCheckBox.includes('groupAutoBeforeClose')) {
        if (testPositiveInt(this.form.configForm.groupJoinNum, 2, 10)) {
          testPositiveInt(value, 1, this.form.configForm.groupJoinNum) ? callback() : callback(new Error(`请输入1~${this.form.configForm.groupJoinNum}`))
        } else {
          testPositiveInt(value, 1) ? callback() : callback(new Error('请输入正整数'))
        }
      } else {
        callback()
      }
    }

    const validSpecialPrice = (rule, value, callback) => {
      if (!this.form.configForm.groupLeaderDiscount && this.$isNull(value)) {
        callback()
      } else if (!test2Decimal(value, 0.01, 99999.99)) {
        callback(new Error('请输入0.01~99999.99'))
      } else {
        callback()
      }
    }

    const validatePerOrderLimit = (rule, value, callback) => {
      console.log(value, this.form.configForm.perUserLimit)
      if (this.$isNull(value)) {
        callback(new Error('请输入1~100'))
      } else if (!testPositiveInt(value, 1, 100)) {
        callback(new Error('请输入1~100'))
      } else if (this.form.configForm.perUserLimitSwitch && testPositiveInt(this.form.configForm.perUserLimit) && Number(value) > Number(this.form.configForm.perUserLimit)) {
        callback(new Error('单笔限购应小于等于顾客限购'))
      } else {
        callback()
      }
    }

    return {
      rules: {
        skuList: [
          {
            type: 'array',
            required: true,
            message: '请选择商品',
            trigger: ['blur', 'change']
          }
        ],
        name: [
          {
            required: true,
            message: '请输入活动名称',
            trigger: ['blur', 'change']
          }
        ],
        activityDate: [
          {
            required: true,
            validator: validateActivityDate,
            trigger: ['blur', 'change']
          }
        ],
        periodTimeList: [
          {
            required: true,
            validator: validatePeriodTimeList,
            trigger: ['blur', 'change']
          }
        ],
        skuPrice: [
          {
            required: true,
            validator: valid2Decimal,
            trigger: ['blur', 'change']
          }
        ],
        specialPrice: [
          {
            required: true,
            validator: validSpecialPrice,
            trigger: ['blur', 'change']
          }
        ],
        skuBaseStock: [
          {
            required: true,
            validator: validPositiveInt,
            trigger: ['blur', 'change']
          }
        ],
        'configForm.groupJoinNum': [
          {
            required: true,
            K_MinNum: 2,
            K_MaxNum: 10,
            K_ErrMsg: '请输入2~10',
            validator: validPositiveInt,
            trigger: ['blur', 'change']
          }
        ],
        'configForm.userLimitCards': [
          {
            required: true,
            validator: validateUserLimitCards,
            trigger: ['blur', 'change']
          }
        ],
        'configForm.perUserLimit': [
          {
            required: true,
            K_MaxNum: 100,
            K_ErrMsg: '请输入1~100正整数',
            validator: validPositiveInt,
            trigger: ['blur', 'change']
          }
        ],
        'configForm.perOrderLimit': [
          {
            required: true,
            validator: validatePerOrderLimit,
            trigger: ['blur', 'change']
          }
          // {
          //   required: true,
          //   K_MaxNum: 100,
          //   K_ErrMsg: '请输入1~100正整数',
          //   validator: validPositiveInt,
          //   trigger: ['blur', 'change']
          // }
        ],
        'configForm.groupAutoRuleCheckBox': [
          {
            required: true,
            type: 'array',
            message: '请至少设置一种规则',
            trigger: ['blur', 'change']
          }
        ],
        'configForm.payDeadline': [
          {
            required: true,
            K_MaxNum: 15,
            K_ErrMsg: '请输入1~15正整数',
            validator: validPositiveInt,
            trigger: ['blur', 'change']
          }
        ],
        bookingTime: [
          {
            required: true,
            validator: validateBookingTime,
            trigger: ['change']
          }
        ],
        'configForm.groupAutoAfterCreate': [
          {
            validator: validateGroupAutoAfterCreate,
            trigger: ['blur', 'change']
          }
        ],
        'configForm.groupAutoBeforeClose': [
          {
            validator: validateGroupAutoBeforeClose,
            trigger: ['blur', 'change']
          }
        ],
        'configForm.groupAutoBeforeNum': [
          {
            validator: validateGroupAutoBeforeNum,
            trigger: ['blur', 'change']
          }
        ],
        groupExpireTimeArray: [
          {
            validator: validateGroupExpireTimeArray,
            trigger: ['blur', 'change']
          }
        ],
        deliveryList: [
          {
            validator: validateDeliveryList,
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  watch: {
    'form.configForm.userLimit'() {
      this.$refs.form.clearValidate(['configForm.userLimitCards'])
    }
  },
  methods: {}
}
